﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Collections;
using System.Globalization;
using DelonixRegiaHotel.Classes;
using DelonixRegiaHotel.DBManager;

namespace DelonixRegiaHotel
{
    public partial class RoomAvailability : Form
    {



        int tempDays;
        DateTime tempChkIn;
        DateTime tempChkOut;
        CheckIn passRoomInformationDetails;

        public RoomAvailability(int days, DateTime checkIn, DateTime checkOut, CheckIn passRoomInfo)
        {

            InitializeComponent();
            tempDays = days;
            tempChkIn = checkIn;
            tempChkOut = checkOut;
            passRoomInformationDetails = passRoomInfo;
        //    lblChkIn.Text = DateTime.Parse(checkIn.ToString()).ToString("yyyy-MM-dd");
        //    lblChkOut.Text = DateTime.Parse(checkOut.ToString()).ToString("yyyy-MM-dd");

            ArrayList documents = new ArrayList();
            ArrayList documents1 = new ArrayList();
            ArrayList nonSmkAvail = new ArrayList();
            ArrayList nonSmkOccupied = new ArrayList();
            ArrayList nonSmkClean = new ArrayList();
            ArrayList smkAvail = new ArrayList();
            ArrayList smkOccupied = new ArrayList();
            ArrayList smkClean = new ArrayList();

            TreeNode NonSmkNode = new TreeNode("Non-Smoking");
            NonSmkNode.Tag = documents;
            nonSmkAvail = DelonixRegiaDB.GetAvailRooms(tempChkIn, tempChkOut, "No");
            TreeNode firstNode = new TreeNode("Available Room");
            TreeNode secondNode = new TreeNode("Occupied Room");
            TreeNode thirdNode = new TreeNode("Scheduled for cleaning");
            documents = new ArrayList();
            Room rm = new Room();
            foreach (var v in nonSmkAvail)
            {
                if(v is Room)
                {
                    rm = (Room)v;
                    documents.Add(new ListViewItem(rm.RoomNo,2));
                    firstNode.Tag = documents;
                }
            }

            documents = new ArrayList();
            nonSmkOccupied = DelonixRegiaDB.GetOccupiedRooms(tempChkIn, tempChkOut, "No");
            foreach (var v in nonSmkOccupied)
            {
                if (v is Room)
                {
                    rm = (Room)v;
                    documents.Add(new ListViewItem(rm.RoomNo, 3));
                    secondNode.Tag = documents;
                }
            }


            documents = new ArrayList();
            nonSmkClean = DelonixRegiaDB.GetScheduleRooms("No");
            foreach (var v in nonSmkClean)
            {
                if (v is Room)
                {
                    rm = (Room)v;
                    documents.Add(new ListViewItem(rm.RoomNo, 4));
                    thirdNode.Tag = documents;
                }
            }

            NonSmkNode.Nodes.Add(firstNode);
            NonSmkNode.Nodes.Add(secondNode);
            NonSmkNode.Nodes.Add(thirdNode);
            treeView1.Nodes.Add(NonSmkNode);
            treeView1.ImageList = imageList1;

            listView1.SmallImageList = imageList1;
            listView1.View = View.SmallIcon;


            TreeNode smkNode = new TreeNode("Smoking");
            
            smkNode.Tag = documents1;
            smkAvail = DelonixRegiaDB.GetAvailRooms(tempChkIn, tempChkOut, "Yes");
            TreeNode firstNode1 = new TreeNode("Available Room");
            TreeNode secondNode1 = new TreeNode("Occupied Room");
            TreeNode thirdNode1 = new TreeNode("Scheduled for cleaning");
           
            documents1 = new ArrayList();

            foreach (var v in smkAvail)
            {
                if (v is Room)
                {
                    rm = (Room)v;
                    documents1.Add(new ListViewItem(rm.RoomNo, 2));
                    firstNode1.Tag = documents1;
                }
            }

            documents1 = new ArrayList();
            smkOccupied = DelonixRegiaDB.GetOccupiedRooms(tempChkIn, tempChkOut, "Yes");
            foreach (var v in smkOccupied)
            {
                if (v is Room)
                {
                    rm = (Room)v;
                    documents1.Add(new ListViewItem(rm.RoomNo, 3));
                    secondNode1.Tag = documents1;
                }
            }
            documents1 = new ArrayList();
            smkClean = DelonixRegiaDB.GetScheduleRooms("Yes");
            foreach (var v in smkClean)
            {
                if (v is Room)
                {
                    rm = (Room)v;
                    documents1.Add(new ListViewItem(rm.RoomNo, 4));
                    thirdNode1.Tag = documents1;
                }
            }

            smkNode.Nodes.Add(firstNode1);
            smkNode.Nodes.Add(secondNode1);
            smkNode.Nodes.Add(thirdNode1);
            treeView1.Nodes.Add(smkNode);
            treeView1.ImageList = imageList1;

            listView1.SmallImageList = imageList1;
            listView1.View = View.SmallIcon;

            listView1.MouseClick += new MouseEventHandler(listView1_MouseClick);


        }

        public void listView1_MouseClick(object sender, MouseEventArgs e)
        {
            ListViewItem click = listView1.GetItemAt(e.X, e.Y);
            if (click != null)
            {
                lblRmNo.Text = listView1.SelectedItems[0].Text;

                ArrayList roomInfo = new ArrayList();
                roomInfo = DelonixRegiaDB.GetRoomDetails(lblRmNo.Text);
                foreach (RoomType rt in roomInfo)
                {
                    lblRoomDetails.Text = rt.RoomDetails;
                    lblRoomPrice.Text = Convert.ToString(rt.RoomPrice);
                    lblRoomTypeName.Text = rt.RoomTypeName;
                }

            }

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            listView1.Items.Clear();

            ArrayList documents = (ArrayList)e.Node.Tag;
            if (documents != null)
            {
                foreach (ListViewItem lvi in documents)
                {
                    listView1.Items.Add(lvi);
                }
            }
        }

        private void treeView1_BeforeCollaspe(object sender, TreeViewEventArgs e)
        {
            e.Node.ImageIndex = 0;
            e.Node.SelectedImageIndex = 0;
        }

        private void treeView1_BeforeExpand(object sender, TreeViewEventArgs e)
        {
            e.Node.ImageIndex = 1;
            e.Node.SelectedImageIndex = 1;
        }


        private void button1_Click(object sender, EventArgs e)
        {

            Room rm = new Room();
            RoomType rmType = new RoomType();
            StaticSession.TempArrayList = DelonixRegiaDB.GetRoomDetails(listView1.SelectedItems[0].Text);
            if (listView1.SelectedItems.Count > 0)
            {
                foreach (var v in StaticSession.TempArrayList)
                {
                    if (v is Room)
                    {
                        rm = (Room)v;
                    }

                    else if (v is RoomType)
                    {
                        rmType = (RoomType)v;
                    }
                }
            }


            FormCollection fc = Application.OpenForms;
            foreach (Form form in fc)
            {
                if (form.Text.Equals("CheckIn"))
                {
                    passRoomInformationDetails.lblRmType.Text = rmType.RoomTypeName;
                    passRoomInformationDetails.lblRmNum.Text = lblRmNo.Text;
                    passRoomInformationDetails.lblRoomPrice.Text = Convert.ToString(rmType.RoomPrice);
                    passRoomInformationDetails.tbxRoomDetails.Text = rmType.RoomDetails;
                    double totalPrice = Convert.ToDouble(tempDays) * Convert.ToDouble(passRoomInformationDetails.lblRoomPrice.Text);
                    passRoomInformationDetails.lblTotal.Text = Convert.ToString(totalPrice);
                    this.Hide();
                }

            }
        }
    }
}
